package model;

import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.net.Socket;

public class SessaoRemota implements Runnable {
	
	private Agencia agencia;
	private int id;
	private Socket socket;
	
	public SessaoRemota(Agencia agencia, int id, Socket socket) {
		this.agencia = agencia;
		this.id = id;
		this.socket = socket;
	}

	@Override
	public void run() {
		ObjectInputStream stream_in;
		ObjectOutputStream stream_out;
		Mensagem msg;
		
		System.out.println("[" + id + "]" + " - Requisicao remota");
		try {
			stream_in = new ObjectInputStream(socket.getInputStream());
			msg = (Mensagem) stream_in.readObject();
			
			StatusMensagemEnum status = msg.executar(agencia, id, this);
			
			stream_out = new ObjectOutputStream(socket.getOutputStream());
			stream_out.writeObject(status);
			this.socket.close();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (ClassNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

}
